package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object>   getDataMapBySkuId(Long skuId) throws ExecutionException, InterruptedException {

        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return productFeignClient.getSkuInfo(skuId).getData();
        }, threadPoolExecutor);

        CompletableFuture<BigDecimal> priceCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return productFeignClient.getSkuPrice(skuId).getData();
        }, threadPoolExecutor);

        CompletableFuture.runAsync(() -> {
            listFeignClient.incrGoodsHotScore(skuId);
        },threadPoolExecutor);

        CompletableFuture<Object> categoryViewCompletableFuture = skuInfoCompletableFuture.thenApplyAsync((skuInfo) -> {
           return productFeignClient.getCategoryView(skuInfo.getCategory3Id()).getData();
        }, threadPoolExecutor);

        CompletableFuture<List<SpuSaleAttr>> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenApplyAsync(skuInfo -> {
            return productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId()).getData();
        }, threadPoolExecutor);

        CompletableFuture<Map<String, Long>> idMappingsCompletableFuture = skuInfoCompletableFuture.thenApplyAsync(skuInfo -> {
            return productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId()).getData();
        }, threadPoolExecutor);


        String idS = JSON.toJSONString(idMappingsCompletableFuture.get());

        Map<String, Object> map = new HashMap<>();
        // 保存 skuInfo
        map.put("skuInfo",skuInfoCompletableFuture.get());
        // 保存 spuSaleAttrList
        map.put("spuSaleAttrList",spuSaleAttrListCompletableFuture.get());
        // 保存 valuesSkuJson
        map.put("valuesSkuJson",idS);
        // 保存价格
        map.put("price",priceCompletableFuture.get());
        // 保存商品分类数据
        map.put("categoryView",categoryViewCompletableFuture.get());
        return map;
    }
}
